package connect

import "system/models"

func AddCourses(courses models.Course) error {
	err := db.Table("course").Select("cno", "cname").Create(&courses).Error
	return err
}

func DelCourses(id string) error {
	err := db.Table("course").Where("cno = ?", id).Delete(models.Course{}).Error
	return err
}

func UpdateCourses(courses models.Course) error {
	err := db.Table("course").Where("cno = ?", courses.Cno).Updates(&courses).Error
	return err
}

func GetAllCourses(coursesParam map[string]interface{}) (error, []models.Course, int64) {
	var coursesData []models.Course
	page := coursesParam["page"].(int)
	pageSize := coursesParam["limit"].(int)
	searchName := coursesParam["searchName"].(string)
	var total int64
	err := db.Table("course").Where("cname like ? ", "%"+searchName+"%").Order("cno ASC").Count(&total).Find(&coursesData).Offset((page - 1) * pageSize).Limit(pageSize).Error
	return err, coursesData, total
}